﻿using LibraryMangementSystem.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMangementSystem.BookManage
{
    public partial class BookAdd : Form
    {
        public BookAdd()
        {
            InitializeComponent();
        }
        LibraryEntities1 dbContext = new LibraryEntities1();
        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            var BookId = textBoxBookId.Text;
            var ISBN = textBoxISBN.Text;
            var Title = textBoxTitle.Text;
            var Publish = dateTimePickerPublish.Value;
            var Author = textBoxAuthor.Text;
            var Words = textBoxWords.Text;
            var PublisherID = comboBoxPublisherId.SelectedValue.ToString();
            var Number = textBoxNumber.Text;
            var Price = textBoxPrice.Text;
            var KeyWord = textBoxKeyWord.Text;
            var Abstract = textBoxAbstract.Text;
            var BookClass = ((ComboBoxItem)comboBoxClass.SelectedItem).Value.ToString();
            if (BookId != "")
            {
                BookInfo bookInfo = new BookInfo();
                bookInfo.BookID = BookId;
                bookInfo.ISBN = ISBN;
                bookInfo.Title = Title;
                bookInfo.Publish = Publish.ToString("yyyy-MM-dd");
                bookInfo.Author = Author;
                bookInfo.PublisherID = PublisherID;
                bookInfo.ClassID = BookClass;
                if (Words != "")
                {
                    bookInfo.Words = int.Parse(Words);
                }
                if(Number != "")
                {
                    bookInfo.Number = int.Parse(Number);
                }
              /*  if(Price != "")
                {
                    bookInfo.Price = decimal.Parse(Price);
                }*/
                bookInfo.Keyword = KeyWord;
                bookInfo.Abstract = Abstract;
                dbContext.BookInfo.Add(bookInfo);
                int result = dbContext.SaveChanges();
                String str = result == 1 ? "添加成功" : "添加失败";
                MessageBox.Show("添加图书成功！");
            }
            else
            {
                MessageBox.Show("图书编号不得为空！");
                return;
            }

        }
        private void comBoxLoad(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“libraryDataSet.Publisherlnfo”中。您可以根据需要移动或删除它。
            this.publisherlnfoTableAdapter.Fill(this.libraryDataSet.Publisherlnfo);
            foreach (var item in dbContext.BookClass)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Text = item.Classname.Trim();
                cbi.Value = item.ClassID;
                comboBoxClass.Items.Add(cbi);
            }
            comboBoxClass.SelectedIndex = 0;
        }
    }
}
